package com.blame.onlinemusicserver.service;


import com.blame.onlinemusicserver.mapper.LoveMusicMapper;
import com.blame.onlinemusicserver.model.Music;
import com.blame.onlinemusicserver.model.User;
import com.blame.onlinemusicserver.tools.Constant;
import com.blame.onlinemusicserver.tools.ResponseBodyMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Slf4j
@Service
public class LoveMusicService {

    @Autowired
    private LoveMusicMapper loveMusicMapper;

    //添加收藏
    public ResponseBodyMessage<Boolean> addLikeMusic(String id,HttpServletRequest request){

        Integer musicId=Integer.parseInt(id);
        System.out.println("musicId"+musicId);

        //1.检查登录
        HttpSession session=request.getSession(false);
        if(session==null || session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            log.error("未登录，请先进行登录");
            return new ResponseBodyMessage<>(0,"没有登录",false);
        }
        User user =(User) session.getAttribute(Constant.USERINFO_SESSION_KEY);

        Integer userId=user.getId();
        System.out.println("userId"+userId);

        Music music=loveMusicMapper.selectMusicIsExist(userId,musicId);

        if(music!=null){
            return new ResponseBodyMessage<>(-1,"该音乐已经加入了收藏夹",false);
        }
        Boolean successAdd=loveMusicMapper.insertLoveMusic(userId,musicId);
        if(successAdd){
            return new ResponseBodyMessage<>(1,"音乐加入收藏夹成功",true);
        }
        return new ResponseBodyMessage<>(-1,"音乐加入收藏夹失败",false);
    }


    //取消收藏
    public ResponseBodyMessage<Boolean> deleteLikeMusic(String id,HttpServletRequest request){
        Integer musicId=Integer.parseInt(id);
        System.out.println("musicId"+musicId);

        //检查登录
        HttpSession session=request.getSession(false);
        if(session==null || session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            log.error("未登录，请先进行登录");
            return new ResponseBodyMessage<>(0,"没有登录",false);
        }
        User user =(User) session.getAttribute(Constant.USERINFO_SESSION_KEY);

        Integer userId=user.getId();
        System.out.println("userId"+userId);
        Music music=loveMusicMapper.selectMusicIsExist(userId,musicId);
        if(music==null){
            return new ResponseBodyMessage<>(-1,"该音乐不在收藏夹内",false);
        }
        Boolean successDelete=loveMusicMapper.deleteLoveMusic(userId,musicId);
        if(successDelete){
            return new ResponseBodyMessage<>(1,"音乐已经从收藏夹中删除",true);
        }
        return new ResponseBodyMessage<>(-1,"音乐从收藏夹中删除失败",false);
    }


    //查询收藏
    public ResponseBodyMessage<List<Music>> findLoveMusic(HttpServletRequest request,String musicName){
        //判断是否登录
        HttpSession session= request.getSession(false);
        if(session==null || session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            log.error("未登录，请先进行登录");
            return new ResponseBodyMessage<>(0,"没有登录",null);
        }
        User user=(User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        Integer userId=user.getId();
        System.out.println("userId"+userId);
        List<Music> musics=null;
        if(userId==null){
            return new ResponseBodyMessage<>(-1,"用户不存在",null);
        }
        log.info("已经找到该用户");
        //查询用户的全部收藏音乐
        if(musicName==null){
            musics= loveMusicMapper.findLoveMusicByUserId(userId);
            //判断 musics 不为空且不为 null
            if(!CollectionUtils.isEmpty(musics)){
                return new ResponseBodyMessage<>(0,"已经找到用户的全部收藏音乐",musics);
            }else {
                return new ResponseBodyMessage<>(-1,"用户的收藏音乐为空",null);
            }
        }else {   //根据用户信息来查询想要查询的收藏的歌曲
            System.out.println("musicName"+musicName);
            musics=loveMusicMapper.findLoveMusicByUserIdAndMusicId(userId,musicName);
            //判断 musics 不为空且不为 null
            if(!CollectionUtils.isEmpty(musics)){
                return new ResponseBodyMessage<>(0,"已经找到用户查询的收藏歌曲",musics);
            }else {
                return new ResponseBodyMessage<>(-1,"用户没有收藏此音乐",null);
            }

        }
    }
}
